﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HyEduOnline.Server.Student;
using HyEduOnline.Entities;
using HyEduOnline.Server.Teacher;
using HyEduOnline.Server.Admin;
using HyEduOnline.Models;
using HyEduOnline.Server.Common;

namespace WebSite.Areas.WebApi.Controllers
{
   
    public class LoginValidateController : BaseController
    {

        private const int StudentType = 0;
        private const int TeacherType = 1;
        private const int MangerType = 2;
        private const int AllType = 3;


        #region 操作cookie

        /// <summary>
        /// 写cookie
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="PassWord"></param>
        /// <param name="UserType"></param>
        private void SetCookie(string UserName, string PassWord, int UserType)
        {
            string passWord = encryption.XOR(PassWord);//密码加密

            HttpCookie cookie = new HttpCookie("User");
            cookie.Values["LoginName"] = UserName;
            cookie.Values["LoginPassWord"] = passWord;
            cookie.Values["UserType"] = UserType.ToString();
            cookie.Expires = System.DateTime.Now.AddDays(3);//设置过期时间3天
            Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 删除指定名称的cookie
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool DelCookie(string name)
        {
            //删除cookie
            HttpCookie cookie = new HttpCookie(name);
            cookie.Expires = System.DateTime.Now.AddDays(-4);
            Response.Cookies.Add(cookie);

            //清空session
            FrontUserInfo.ClearSession();
            return true;
        }

        #endregion

        #region 教师登录验证
        /// <summary>
        /// 教师登录验证
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public int TeacherLogin(string UserName, string PassWord)
        {


            if ((!string.IsNullOrEmpty(UserName))&&(!string.IsNullOrEmpty(PassWord)))
            {
                SetCookie(UserName, PassWord, TeacherType);//写入cookie 
                PassWord = Utitil.MD5(PassWord);//对密码加密

                if (LoginValidate.ValidateTeacher(UserName, PassWord))
                {                    
                    return -4;//登陆成功
                }
                return 0;
            }
            else
            {
                return -3;
            }
        }

        #endregion

        #region 学生登录验证
        /// <summary>
        /// 学生登录
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="PassWord"></param>
        /// <returns></returns>
        [HttpPost]
        public int StudentLogin(string UserName, string PassWord)
        {
            if ((!string.IsNullOrEmpty(UserName)) && (!string.IsNullOrEmpty(PassWord)))
            {
                SetCookie(UserName, PassWord, StudentType);//写入cookie
                PassWord = Utitil.MD5(PassWord);//对密码加密

                if (LoginValidate.ValidateStudent(UserName, PassWord))
                {                   
                    string paperID = ValidateStuTest();
                    if (string.IsNullOrEmpty(paperID))
                        return -4;//登陆成功
                    else
                    {
                        Session["paper"] = paperID;
                        return 1;//有考试
                    }
                }
                return 0;
            }
            else
            {
                return -3;
            }
        }

        /// <summary>
        /// 判断当前登录学生是否有考试
        /// </summary>
        /// <param name="UserName"></param>
        public string ValidateStuTest()
        {
            //查找当前学生的考试
            TList<TbTestStudents> testList = new TestStudentsDAO().GetByPage(
                "I_StuID=" + frontCurrentStu.GetUserID(), 0, int.MaxValue);
            if (testList.Count != 0)
            {
                foreach (var item in testList)
                {
                    TbTestLibPaper paper = new TestLibPaperDAO().GetByID((int)item.IPaperId);
                    if (paper == null)
                        return "";
                    //考前等待时间
                    int waitTime = (int)paper.IWaitTime;
                    //考试开始时间
                    DateTime testTime = (DateTime)item.DTestTime;
                    //考试过程占用时间,min
                    int overTime = 0 - (int)new TestLibPaperDAO().GetByID((int)item.IPaperId).IOverTime;
                    TimeSpan tt = testTime - DateTime.Now;//登录时距离考试的时间,考试时间-登录时间

                    int timeTemp = (tt.Days * 24 + tt.Hours) * 60 + tt.Minutes;//转换为分钟
                    if (item.ICompleted == 1)//若考试已提交试卷
                        continue;
                    if (waitTime < timeTemp)
                        continue;
                    else if (overTime > timeTemp && timeTemp < 0)
                        continue;
                    else
                        return item.IPaperId.ToString();
                }
            }
            return "";
        }
      
        #endregion
    }
}
